package com.mall.controller;

import com.alibaba.fastjson.JSON;
import com.mall.bean.SysUser;
import com.mall.bean.User;
import com.mall.interceptor.Log;
import com.mall.service.SysUserService;
import com.mall.service.SystemPermissionService;
import com.mall.service.UserService;
import com.mall.util.SecurityUtil;
import com.mall.vo.MenuVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sound.midi.Soundbank;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaochao on 2017/7/23.
 */
@Controller
@RequestMapping("/login")
public class LoginController {

    private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private SystemPermissionService SystemPermissionService;

    @Log(model = "用户登录",describe = "系统用户登录后台管理系统")
    @RequestMapping("/gotoLogin")
    public ModelAndView gotoLogin(String username, String password,HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        if(username == null || password == null){
            map.put("msg","用户名密码不能为空");
            return new ModelAndView("redirect:/login.jsp", map);
        }
        Subject subject = SecurityUtils.getSubject();
        try {
            // 回调doGetAuthenticationInfo，进行认证
            UsernamePasswordToken token = new UsernamePasswordToken(username,SecurityUtil.getEncryotString(password,username));
            subject.login(token);
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }
        //验证是否通过
        if (subject.isAuthenticated()) {
            SysUser user = (SysUser) subject.getSession().getAttribute("user");
            List<MenuVo> userPermission = SystemPermissionService.getUserPermission(user.getId());
            map.put("user", user);
            System.out.println("-------------------------------"+JSON.toJSON(userPermission)+"------------------------");
            map.put("userPermission", JSON.toJSON(userPermission));
            return new ModelAndView("/index", map);
        } else {
            map.put("msg", "用户名或密码不正确");
        }
        return new ModelAndView("redirect:/login.jsp", map);
    }

    /**
     * 跳转欢迎页面
     */
    @GetMapping("/gotoWelcome")
    public ModelAndView gotoWelcome() {
        Map<String, Object> map = new HashMap<String, Object>();
        Subject sub = SecurityUtils.getSubject();
        SysUser user = (SysUser) sub.getSession().getAttribute("user");
        map.put("user", user);
        return new ModelAndView("/welcome", map);
    }


    /**
     * 退出登录
     */
    @GetMapping("/logout")
    public ModelAndView logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return new ModelAndView("redirect:/login.jsp");
    }

    /**
     * 首页欢迎搜索页
     */
    @GetMapping("/getSearch")
    public String getSearch(){
        return "/search";
    }






}
